//
//  ViewController.m
//  ScheduleDemo
//
//  Created by 赵博 on 2017/12/15.
//  Copyright © 2017年 个人. All rights reserved.
//

#import "ScheduleTool.h"
#import <EventKit/EventKit.h>
@interface ScheduleTool ()
{
    EKEventStore *store;
}
@end

@implementation ScheduleTool

- (instancetype)init{
    self = [super init];
    if (self) {
        store = [[EKEventStore alloc] init];
    }
    return self;
}



#pragma mark - 查询日程
- (void)getSchedule:(NSString *)keyword{
    // 获取日历对象
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // 创建结束时间
    NSDateComponents *oneYearFromNowComponents = [[NSDateComponents alloc] init];
    oneYearFromNowComponents.year = 1;
    NSDate *oneDayAgo = [calendar dateByAddingComponents:oneYearFromNowComponents toDate:[NSDate date] options:0];
    NSDate *oneYearFromNow = [calendar dateByAddingComponents:oneYearFromNowComponents toDate:[NSDate date] options:0];
    NSPredicate *predicate;
    //如果没有关键字则获取1年的日程
    if (keyword == nil) {
        predicate = [store predicateForEventsWithStartDate:oneDayAgo endDate:oneYearFromNow calendars:nil];
    }else{
         predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",keyword];
    }
    // 获得符合条件的所有日程
    NSArray *events = [store eventsMatchingPredicate:predicate];
    //批量处理所搜到的日程
//    [store enumerateEventsMatchingPredicate:predicate usingBlock:^(EKEvent * _Nonnull event, BOOL * _Nonnull stop) {
//        NSLog(@"event:%@",event.title);
//        //获取唯一标识eventIdentifier(用系统指定)
//    }];
    NSLog(@"%@",events);
}


#pragma mark - 添加日程
- (void)addSchedule:(NSString *)title stratTimeStr:(NSString *)stratTimeStr endTimeStr:(NSString *)endTimeStr note:(NSString *)note isRepetition:(BOOL)isRepetition repetition:(NSInteger)repetition recurrenceEndStr:(NSString *)recurrenceEndStr{
    EKEvent *event = [EKEvent eventWithEventStore:store];
    event.title = title;
    event.calendar = [store defaultCalendarForNewEvents];
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
    dateFormatter.dateFormat=@"yyyy-MM-dd HH:mm:ss";
    
    //开始时间
    NSDate *startTime = [dateFormatter dateFromString:stratTimeStr];
    //结束时间
    NSDate *endTime=[dateFormatter dateFromString:endTimeStr];
    //重复结束时间
    NSDate *recurrenceDate;
    if (recurrenceEndStr == nil || [recurrenceEndStr isEqualToString:@""] ) {
        recurrenceDate = nil;
    }else{
        recurrenceDate = [dateFormatter dateFromString:recurrenceEndStr];
    }
   
    event.startDate = startTime;
    event.endDate = endTime;
    event.notes = note;
    if (isRepetition) {
        for (EKRecurrenceRule *rulein in event.recurrenceRules) {
            [event removeRecurrenceRule:rulein];
        }
        EKRecurrenceEnd *recurrenceEnd = [EKRecurrenceEnd recurrenceEndWithEndDate:recurrenceDate];
        EKRecurrenceRule *rule = [[EKRecurrenceRule alloc]initRecurrenceWithFrequency:repetition interval:1  end:recurrenceEnd];
     
        
        [event addRecurrenceRule:rule];
    }

    [event addAlarm:[EKAlarm alarmWithRelativeOffset:-10*60]];
    
    NSError *error;
    [store saveEvent:event span:EKSpanFutureEvents commit:YES  error:&error];
    if (!error) {
        NSLog(@"添加成功！");
    }else{
        NSLog(@"添加失败：%@",error);
    }
}
#pragma mark 删除日程
- (void)deleSchedule:(EKEvent *)event{
    NSError *error;
    [store removeEvent:event span:EKSpanFutureEvents error:&error];
}



@end
